//
//  MeInfoMainHeadView.m
//  MPTLive
//
//  Created by HouGeng on 16/1/28.
//  Copyright © 2016年 Jeakin. All rights reserved.
//

#import "MeInfoMainHeadView.h"

#define Kong (15)

@interface MeInfoMainHeadView ()
<
UIScrollViewDelegate
>

@property (nonatomic, strong) UIScrollView *scrV;

/// 图片
@property (nonatomic, strong) TPImageView *imageView;

/// 被赞
@property (nonatomic, strong) TPLabel *labBeiZan;

/// 名字
@property (nonatomic, strong) TPLabel *labName;

/// 编辑
@property (nonatomic, strong) TPButton *butBianJi;

/// 点点
@property (nonatomic, strong) UIPageControl *pageC;

/// 直播
@property (nonatomic, strong) TPLabel *labLive;

/// 关注
@property (nonatomic, strong) TPLabel *labGuanZhu;

/// 粉丝
@property (nonatomic, strong) TPLabel *labFenShi;

/// 签名
@property (nonatomic, strong) TPLabel *labQianMing;

@property (nonatomic, strong) UIView *view;

/// 大V
@property (nonatomic, strong) TPImageView *imageBigV;

@property (nonatomic, strong) NSMutableDictionary *obj;

@end


@implementation MeInfoMainHeadView

- (id)initWithFrame:(CGRect)frame
{
    self =[super initWithFrame:frame];
    
    if (self)
    {
        ///  图片
        _imageView = [TPImageView getWithFrame:CGRectMake(0, 0, screenW, screenW)];
        [self addSubview:_imageView];
        
        /// 下面遮罩
        TPImageView *imageV = [TPImageView getWithFrame:CGRectMake(0, screenW - 73, screenW, 73) image:[UIImage imageNamed:@"mengchengxia"]];
        [self addSubview:imageV];
        
        /// 下面的滚试图
        _scrV = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, screenW, screenW)];
        _scrV.backgroundColor = [UIColor clearColor];
        _scrV.delegate = self;
        _scrV.contentSize = CGSizeMake(screenW * 2, 0);
        _scrV.pagingEnabled = YES;
        _scrV.showsHorizontalScrollIndicator = NO;
        _scrV.showsVerticalScrollIndicator = NO;
        [self addSubview:_scrV];
        
        _view = [[UIView alloc] initWithFrame:CGRectMake(-screenW, 0, screenW * 4, screenW)];
        _view.backgroundColor = [UIColor blackColor];
        _view.alpha = 0;
        [_scrV addSubview:_view];
        
        /// 名称
        _labQianMing = [TPLabel getWithFrame:CGRectMake((screenW - 150)/ 2 + screenW, (screenW - 150)/ 2, 150, 150)
                                        text:@""
                                   textColor:RGBAAllColor(0xFFFFFF, 1)
                                    textFont:[UIFont fontWithName:@"FZLTXHK--GBK1-0" size:14.0f]
                               textAlignment:NSTextAlignmentCenter];
        _labQianMing.numberOfLines = 0;
        [_scrV addSubview:_labQianMing];
        
        
        /// 被赞
        _labBeiZan = [TPLabel getWithFrame:CGRectMake(Kong, screenW - 35, 200, 15)
                      text:@"" textColor:RGBAAllColor(0xffe200, 1)
                                  textFont:[UIFont fontWithName:@"FZLTXHK--GBK1-0" size:13.0f]
                             textAlignment:NSTextAlignmentLeft];
        [_scrV addSubview:_labBeiZan];
        
        /// 大V
        _imageBigV = [TPImageView getWithFrame:CGRectMake(Kong, CGRectGetMinY(_labBeiZan.frame) -27, 17, 17) image:[UIImage imageNamed:@"bigV"]];
        _imageBigV.hidden = YES;
        [_scrV addSubview:_imageBigV];
        
        /// 名称
        _labName = [TPLabel getWithFrame:CGRectMake(CGRectGetMaxX(_imageBigV.frame)+7, CGRectGetMinY(_labBeiZan.frame) -27, 200, 17)
                                     text:@"" textColor:RGBAAllColor(0xFFFFFF, 1)
                                 textFont:[UIFont fontWithName:@"FZLTXHK--GBK1-0" size:18.0f]
                            textAlignment:NSTextAlignmentLeft];
        [_scrV addSubview:_labName];
        
        /// 编辑
        _butBianJi = [TPButton getWithFrame:CGRectMake(screenW - 58 - 15, screenW - 30 -28, 58, 35) target:self action:@selector(butClicked:)];
        _butBianJi.tag = BianJiC;
        
        [_scrV addSubview:_butBianJi];
        
        /// 最下面一条
        CGFloat fltH = 69 / 2;
        CGFloat fltW = screenW / 3.0;
        
        /// 底色view
        UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, screenW, fltW, 64)];
        view1.backgroundColor = [UIColor whiteColor];
        [self addSubview:view1];
        
        UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(fltW, screenW, fltW * 2, 64)];
        view2.backgroundColor =  RGBAllColor(0xF1F1F1);
        [self addSubview:view2];
        
        
        
        TPLabel *lab1 = [TPLabel getWithFrame:CGRectMake(0, screenW + 5, fltW, fltH)
                                    text:@"直播" textColor:RGBAAllColor(0x282828, 1)
                                textFont:[UIFont fontWithName:@"FZLTXHK--GBK1-0" size:14.0f]
                           textAlignment:NSTextAlignmentCenter];
        [self addSubview:lab1];
        
        TPLabel *lab2 = [TPLabel getWithFrame:CGRectMake(fltW, screenW + 5, fltW, fltH)
                                         text:@"关注" textColor:RGBAAllColor(0x282828, 1)
                                     textFont:[UIFont fontWithName:@"FZLTXHK--GBK1-0" size:14.0f]
                                textAlignment:NSTextAlignmentCenter];
        [self addSubview:lab2];
        
        TPLabel *lab3 = [TPLabel getWithFrame:CGRectMake(fltW * 2, screenW + 5, fltW, fltH)
                                         text:@"粉丝" textColor:RGBAAllColor(0x282828, 1)
                                     textFont:[UIFont fontWithName:@"FZLTXHK--GBK1-0" size:14.0f]
                                textAlignment:NSTextAlignmentCenter];
        [self addSubview:lab3];
        
        _labLive = [TPLabel getWithFrame:CGRectMake(0, screenW + fltH - 7, fltW, fltH)
                                         text:@"" textColor:RGBAAllColor(0x282828, 1)
                                     textFont:[UIFont fontWithName:@"FZLTXHK--GBK1-0" size:14.0f]
                                textAlignment:NSTextAlignmentCenter];
        [self addSubview:_labLive];
        
        _labGuanZhu = [TPLabel getWithFrame:CGRectMake(fltW, screenW + fltH - 7, fltW, fltH)
                                         text:@"" textColor:RGBAAllColor(0x282828, 1)
                                     textFont:[UIFont fontWithName:@"FZLTXHK--GBK1-0" size:14.0f]
                                textAlignment:NSTextAlignmentCenter];
        [self addSubview:_labGuanZhu];
        
        _labFenShi = [TPLabel getWithFrame:CGRectMake(fltW * 2, screenW + fltH - 7, fltW, fltH)
                                         text:@"" textColor:RGBAAllColor(0x282828, 1)
                                     textFont:[UIFont fontWithName:@"FZLTXHK--GBK1-0" size:14.0f]
                                textAlignment:NSTextAlignmentCenter];
        [self addSubview:_labFenShi];
        
        /// 关注 粉丝点击事件
        TPButton *butGuanZhu = [TPButton getWithFrame:CGRectMake(fltW , screenW, fltW, 69) target:self action:@selector(butClicked:)];
        butGuanZhu.backgroundColor = [UIColor clearColor];
        butGuanZhu.tag = GuanZhuA;
        [self addSubview:butGuanZhu];
        
        TPButton *butFenShi = [TPButton getWithFrame:CGRectMake(fltW * 2, screenW, fltW, 69) target:self action:@selector(butClicked:)];
        butFenShi.backgroundColor = [UIColor clearColor];
        butFenShi.tag = FenShiB;
        [self addSubview:butFenShi];
        
        /// 点点
        _pageC = [[UIPageControl alloc] initWithFrame:CGRectMake(0, screenW + 53 - 69 - 10, screenW, 20)];
        _pageC.numberOfPages = 2;
        [self addSubview:_pageC];
    }
    
    return self;
}

- (void)isShowBianJi:(BOOL)isBool
{
    _isShowGuanzhu = isBool;
    
    if (_isShowGuanzhu)
    {
        [_butBianJi setImage:[UIImage imageNamed:@"bianjiN"] forState:UIControlStateNormal];
        [_butBianJi setImage:[UIImage imageNamed:@"bianjiS"] forState:UIControlStateHighlighted];
    }
    else
    {
        /// 关注
        [_butBianJi setTitle:@"" forState:UIControlStateNormal];
    }
}

- (void)setData:(NSDictionary *)obj
{
    _obj = [NSMutableDictionary dictionaryWithDictionary:obj];
    
     [_imageView sd_setImageWithURL:[NSURL URLWithString:obj[@"header"][@"icon"]] placeholderImage:nil];
    [_imageView sd_setImageWithURL:[NSURL URLWithString:obj[@"header"][@"oldIcon"]] placeholderImage:nil];
    
    /// 直播次数
    _labLive.text = [obj[@"header"][@"liveCnt"][@"total"] description];
    
    /// 关注数量
    _labGuanZhu.text = [obj[@"header"][@"eventCnt"][@"follow"] description];
    
    /// 粉丝数量
    _labFenShi.text = [obj[@"header"][@"eventCnt"][@"fans"] description];
    
    /// 名称
    _labName.text = obj[@"header"][@"nick"];
    
    /// 被赞
    _labBeiZan.text = [NSString stringWithFormat:@"被赞%@次",obj[@"header"][@"liveCnt"][@"like"]];
    [_labBeiZan setTextColor:RGBAllColor(0xCFCFCF) textFont:[UIFont systemFontOfSize:13.0f] range:[_labBeiZan.text rangeOfString:@"被赞"]];
    [_labBeiZan setTextColor:RGBAllColor(0xCFCFCF) textFont:[UIFont systemFontOfSize:13.0f] range:[_labBeiZan.text rangeOfString:@"次"]];
    
    /// 签名
    _labQianMing.text = ZB_Str_Is_Valid(obj[@"header"][@"ext"][@"desc"])? obj[@"header"][@"ext"][@"desc"]:@"居然没有签名 -_- !!!";
    
    /// 判断是否显示大V
    _imageBigV.hidden = [obj[@"header"][@"v"] integerValue]== 0;
    
    /// 改变名字位置(侯庚写的)
    _labName.frame = [obj[@"header"][@"v"] integerValue]== 0 ? CGRectMake(Kong, CGRectGetMinY(_labBeiZan.frame) -27, 200, 17) : CGRectMake(CGRectGetMaxX(_imageBigV.frame)+7, CGRectGetMinY(_labBeiZan.frame) -27, 200, 17);
    
    if (!_isShowGuanzhu)
    {
        /// 关注状态
        if ([_obj[@"relation"] integerValue] == 0 || [_obj[@"relation"] integerValue] == 2)
        {
            [_butBianJi setImage:[UIImage imageNamed:@"headweiguanz"] forState:UIControlStateNormal];
            [_butBianJi setImage:[UIImage imageNamed:@"headyiguanz"] forState:UIControlStateSelected];
        }
        else
        {
            [_butBianJi setImage:[UIImage imageNamed:@"headyiguanz"] forState:UIControlStateNormal];
            [_butBianJi setImage:[UIImage imageNamed:@"headweiguanz"] forState:UIControlStateSelected];
        }
    }
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    _pageC.currentPage = scrollView.contentOffset.x / screenW;
    _view.alpha = scrollView.contentOffset.x / (screenW + 200);

}

- (void)butClicked:(TPButton *)sender
{
    if (!_isShowGuanzhu && sender.tag != FenShiB && sender.tag != GuanZhuA)
    {
         if ([_obj[@"relation"] integerValue] != 0 && [_obj[@"relation"] integerValue] != 2)
         {
             /// 提示
             UIAlertView *alent = [[UIAlertView alloc] initWithTitle:@"确认不再关注TA吗?" message:@"" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确认", nil];
             [alent show];
             return;
         }
        [_obj setValue:@(1) forKey:@"relation"];
    }
    sender.selected = !sender.selected;
    if (self.block)
    {
        self.block (sender.tag);
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    switch (buttonIndex)
    {
        case 0:
        {
            NSLog(@"Cancel Button Pressed");
            
            break;
        }
        default:
        {
            [_obj setValue:@(0) forKey:@"relation"];
            _butBianJi.selected = !_butBianJi.selected;
            if (self.block)
            {
                self.block (_butBianJi.tag);
            }
            break;
        }
    }
}

@end
